package com.mes.biz.service.impl;

import java.util.List;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.framework.cache.UserUtils;
import com.mes.common.core.domain.entity.SysUser;
import com.mes.common.utils.StringUtils;
import com.mes.biz.mapper.BizApprovalSettingMapper;
import com.mes.biz.domain.BizApprovalSetting;
import com.mes.biz.service.BizApprovalSettingService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 审批权限设置Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-05-01
 */
@Service
@Transactional(readOnly = true)
public class BizApprovalSettingServiceImpl extends BaseServiceImpl<BizApprovalSettingMapper, BizApprovalSetting> implements BizApprovalSettingService {

    private static final Logger log = LoggerFactory.getLogger(BizApprovalSettingServiceImpl.class);

    /**
     * 获取单条数据
     * @param bizApprovalSetting 审批权限设置
     * @return 审批权限设置
     */
    @Override
    public BizApprovalSetting get(BizApprovalSetting bizApprovalSetting) {
        BizApprovalSetting dto = super.get(bizApprovalSetting);
        if (StringUtils.isNotEmpty(dto.getApprovePerson())){
            String[] approvePersonArr = dto.getApprovePerson().split(";");
            for(String approvePerson:approvePersonArr) {
                SysUser sysUser = UserUtils.getSysUser(approvePerson);
                if (null != sysUser){
                    if(dto.getApprovePersonName()!=null) {
                        dto.setApprovePersonName(dto.getApprovePersonName()+";");
                        dto.setApprovePersonName(dto.getApprovePersonName()+sysUser.getName());
                    } else {
                        dto.setApprovePersonName(sysUser.getName());
                    }
                }
            }
        }
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 审批权限设置id
     * @return 审批权限设置
     */
    @Override
    public BizApprovalSetting get(String id) {
        BizApprovalSetting dto = super.get(id);
        if (StringUtils.isNotEmpty(dto.getApprovePerson())){
            String[] approvePersonArr = dto.getApprovePerson().split(";");
            for(String approvePerson:approvePersonArr) {
                SysUser sysUser = UserUtils.getSysUser(approvePerson);
                if (null != sysUser){
                    if(dto.getApprovePersonName()!=null) {
                        dto.setApprovePersonName(dto.getApprovePersonName()+";");
                        dto.setApprovePersonName(dto.getApprovePersonName()+sysUser.getName());
                    } else {
                        dto.setApprovePersonName(sysUser.getName());
                    }
                }
            }
		}
        return dto;
    }

    /**
     * 查询审批权限设置列表
     * @param bizApprovalSetting 审批权限设置
     * @return 审批权限设置
     */
    @Override
    public List<BizApprovalSetting> findList(BizApprovalSetting bizApprovalSetting) {
		List<BizApprovalSetting> bizApprovalSettingList = super.findList(bizApprovalSetting);
		if (!CollectionUtils.isEmpty(bizApprovalSettingList)) {
			bizApprovalSettingList.stream().forEach(dto -> {
				if (StringUtils.isNotEmpty(dto.getApprovePerson())){
                    String[] approvePersonArr = dto.getApprovePerson().split(";");
                    for(String approvePerson:approvePersonArr) {
                        SysUser sysUser = UserUtils.getSysUser(approvePerson);
                        if (null != sysUser){
                            if(dto.getApprovePersonName()!=null) {
                                dto.setApprovePersonName(dto.getApprovePersonName()+";");
                                dto.setApprovePersonName(dto.getApprovePersonName()+sysUser.getName());
                            } else {
                                dto.setApprovePersonName(sysUser.getName());
                            }
                        }
                    }
				}
			});
		}
        return bizApprovalSettingList;
    }

    /**
     * 分页查询审批权限设置列表
     * @param bizApprovalSetting 审批权限设置
     * @return 审批权限设置
     */
    @Override
    public PageInfo<BizApprovalSetting> findPage(BizApprovalSetting bizApprovalSetting) {
		return super.findPage(bizApprovalSetting);
    }

    /**
     * 保存审批权限设置
     * @param bizApprovalSetting
     * @return 结果
     */
    @Override
    public boolean save(BizApprovalSetting bizApprovalSetting) {
        return super.save(bizApprovalSetting);
    }

    /**
     * 删除审批权限设置信息
     * @param bizApprovalSetting
     * @return 结果
     */
    @Override
    public boolean remove(BizApprovalSetting bizApprovalSetting) {
        return super.remove(bizApprovalSetting);
    }

    /**
     * 批量删除审批权限设置
     * @param ids 需要删除的审批权限设置ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBizApprovalSettingByIds(String[] ids) {
        return mapper.deleteBizApprovalSettingByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

}
